//
//  EduViewController.m
//  edu
//
//  Created by baobaodp on 13-8-12.
//  Copyright (c) 2013年 baobaodp. All rights reserved.
//

#import "EduViewController.h"
#import "LoginViewController.h"
#import "MainViewController.h"
#import "Navbar.h"
#import "NotificationViewController.h"
#import "CommonTableViewController.h"
#import "DBbase.h"

EduViewController *baseViewController;
UserEntry *userEntry;

@interface EduViewController ()

@end

@implementation EduViewController

@synthesize loginViewController;
@synthesize mainViewController;
@synthesize logoImage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    baseViewController = self;
    
    dBbase = [[DBbase alloc] initWithDBName:@"edu"];
    
    [NSThread detachNewThreadSelector:@selector(loadingThread) toTarget:self withObject:nil];
    
    
}

- (void)loadingThread
{
    loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    [self.view insertSubview:loginViewController.view atIndex:0];
    
    
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MainViewController"
                                                 owner:self options:nil];
    for (id oneObject in nib)
        if ([oneObject isKindOfClass:[MainViewController class]])
            mainViewController = (MainViewController *)oneObject;
    
//    CGRect rect2 = mainViewController.view.frame;
//    rect2.origin.y = rect2.origin.y - 20;
//    mainViewController.view.frame = rect2;
    
    [self.view insertSubview:mainViewController.view atIndex:1];
    
    [self.view bringSubviewToFront:loginViewController.view];
    
    [self performSelectorOnMainThread:@selector(didLoad) withObject:nil waitUntilDone:YES];
}

- (void)didLoad
{
//    [UIView animateWithDuration:0.7 //速度0.7秒
//                     animations:^{//修改rView坐标
//                         logoImage.frame = CGRectMake(self.view.frame.origin.x,
//                                                  -self.view.frame.size.height,
//                                                  self.view.frame.size.width,
//                                                  self.view.frame.size.height);
//                     }
//                     completion:^(BOOL finished){
//                     }];
    sleep(1);
    logoImage.hidden = YES;

}

- (void) doLgoin
{
    [self.view bringSubviewToFront:mainViewController.view];
    
    NotificationViewController* nController =  (NotificationViewController*)((UINavigationController *)[mainViewController.viewControllers objectAtIndex:0]).topViewController;


    [nController resetViewedCells];
    [nController.mainTableView reloadData];
    
    
    //[NSThread detachNewThreadSelector:@selector(loginThread) toTarget:self withObject:nil];
    
}

- (void)loginThread
{

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MainViewController"
                                                 owner:self options:nil];
    for (id oneObject in nib)
        if ([oneObject isKindOfClass:[MainViewController class]])
            mainViewController = (MainViewController *)oneObject;
    
    CGRect rect2 = mainViewController.view.frame;
    rect2.origin.y = rect2.origin.y - 20;
    mainViewController.view.frame = rect2;
    
    [self.view insertSubview:mainViewController.view atIndex:1];
    
    [self performSelectorOnMainThread:@selector(didLogin) withObject:nil waitUntilDone:YES];
}

- (void)didLogin
{
    [self.view bringSubviewToFront:mainViewController.view];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
